home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / callback.001 / callback~ / callback / cb / build next >
Text File  |  1996-07-23  |  11KB  |  449 lines

  1. #!/usr/local/bin/icmake -qt /tmp/cb
  2. /*                              build
  3.  
  4.    A C++ makefile generated by C++ for cb.cc
  5.  
  6. Configurable defines for the build script:
  7.  
  8.     BIFLEX:         only to be defined if the biflex-script will be used.
  9.     CLASSES:        string of directory-names under which sources of classes
  10.                     are found
  11.     COMPILER:       "g++" for C++ sources. Do not change this, unless you're
  12.                     sure you want something else.
  13.     COPT:           C-options used by COMPILER
  14.     ECHO_REQUEST:   ON (default) if command echoing is wanted, otherwise: set 
  15.                     to OFF
  16.     GDB:            define if gdb-symbolic debug information is wanted
  17.                     (not defined by default)
  18.     LIBS:           Extra libraries used for linking
  19.     LIBPATH:        Extra library-searchpaths used for linking
  20.     INTERACTIVE:    define only if, in the context of BIFLEX, an interactive
  21.                     scanner is wanted
  22.     NO_LIBRARY:     define this if you don't want a library for the object
  23.                     modules, but want to keep them in separate ./o directories
  24.             by default: not defined, so a library is used.
  25.     PROGRAM:        define if a program is to be built. If not defined,
  26.                     library maintenance is assumed.
  27.                     (default: defined)
  28.                     
  29.     Current values:
  30. */
  31.  
  32. //#define BIFLEX
  33.  
  34. //Do not change the next define unless you know what you're doing:
  35. #define COMPILER "gcc"
  36.  
  37. // COPT: the set of C-options
  38. #define COPT "-m486 -Wall -funsigned-char"
  39.  
  40. // CLASSES: the list of directories containing the sources of classes. E.g.,
  41. //      #define CLASSES "class1 class2"
  42. //  All class-names must be stored in one string, no string-concatenation.
  43. //  If classes are removed from the CLASSES definition or if the names
  44. //  in the CLASSES definition are reordered, the compilation should start 
  45. //  again from scratch.
  46. #define CLASSES ""
  47.  
  48. #define ECHO_REQUEST ON
  49.  
  50. // #define GDB
  51. // #define INTERACTIVE
  52.  
  53. // Extra libraries required. Remove lib and .a from the library names.
  54. // E.g., #define LIBS "m Xt" to link libm.a and libXt.a explicitly
  55. // Specify libs from the most specific to the most general one.
  56. #define LIBS "rss"
  57.  
  58. // Extra library-paths required. 
  59. // E.g., #define LIBPATH "/some/path /some/other/path" to search these paths
  60. // apart from the default paths
  61. #define LIBPATH "../lib"
  62.  
  63. #define NO_LIBRARY
  64. #define PROGRAM
  65.  
  66.  
  67. //      DO NOT ALTER THINGS BELOW THIS LINE
  68. string                                      // contain options for
  69.     libs,                                // extra libs, e.g., "-lrss -licce"
  70.     libpath,                       // extra lib-paths, eg, "-L../rss"
  71.     copt,
  72.     lopt,
  73.     libxxxa;                    // expanded lib-name
  74. int
  75.     relink;                                 // internally used: != 0 to relink
  76.  
  77. string
  78.     ofiles,                             // wildcards for o-files
  79.     sources,                // sources to be used
  80.     wild,                               // wildcard of extension
  81.     current;                            // contains name of current dir.
  82. /*
  83.                                 O B J F I L E S . I M
  84. */
  85.  
  86. list objfiles(list files)
  87. {
  88.     string
  89.         file,
  90.         objfile;
  91.     int
  92.         i;
  93.  
  94.     for (i = 0; i < sizeof(files); i++)
  95.     {
  96.         file = element(i, files);           // determine element of the list
  97. #ifdef NO_LIBRARY
  98.         objfile = "./o/" + change_ext(file, "o");    // make obj-filename
  99. #else
  100.         objfile = change_ext(file, "o");    // make obj-filename
  101. #endif
  102.         if (objfile younger file)           // objfile is younger
  103.         {
  104.             files -= (list)file;            // remove the file from the list
  105.             i--;                            // reduce i to test the next
  106.         }
  107.     }
  108.     return (files);
  109. }
  110. /*
  111.                                  biflex.im
  112. */             
  113.  
  114. void biflex()
  115. {
  116.     chdir("biflex");
  117.     
  118.     if ("parser" younger "parser.tab.c")        // new parser needed
  119.     {
  120.         exec("bison", "-d", "parser");
  121.         printf("Note: the compilation of parser.cc may produce "
  122.            "several compiler warnings.\n");
  123.     }
  124.         
  125.     if 
  126.     (                                           // new lexer needed
  127.         "lexer" younger "lex.yy.c"
  128.         ||
  129.         "parser.tab.h" younger "lex.yy.c" 
  130.     )
  131.     {
  132.         exec("flex",
  133. #ifdef INTERACTIVE
  134.                     "-I", 
  135. #endif
  136.                     "lexer");
  137.                     
  138.         printf("Note: the compilation of lexer.cc may produce "
  139.            "several compiler warnings.\n");
  140.     }
  141.                     
  142.     chdir("..");
  143. }
  144.  
  145. /*
  146.                                 A L T E R E D . I M
  147. */
  148.  
  149. list altered(list files, string target)
  150. {
  151.     int
  152.         i;
  153.     string
  154.         file;
  155.  
  156.     for (i = 0; i < sizeof(files); i++)     // try all elements of the list
  157.     {
  158.         file = element(i, files);           // use element i of the list
  159.             
  160.         if (file older target)              // a file is older than the target
  161.         {
  162.             files -= (list)file;            // remove the file from the list
  163.             i--;                            // reduce i to inspect the next
  164.         }                                   // file of the list
  165.     }
  166.     return (files);                         // return the new list
  167. }
  168. /*
  169.                             F I L E L I S T . I M
  170. */
  171.  
  172. list file_list(string type, string library)
  173. {
  174.     list
  175.         files;
  176.  
  177.     files = makelist(type);                 // make all files of certain type
  178. #ifndef NO_LIBRARY
  179.     files = altered(files, library);        // keep all files newer than lib.
  180. #endif
  181.     files = objfiles(files);                // remove if younger .obj exist
  182.  
  183.     return (files);
  184. }
  185. /*
  186.                         L I N K . I M
  187. */
  188.  
  189. void link(string library, string exe)
  190. {
  191.     if
  192.     (
  193.         relink                           // new library, new main.obj
  194.         ||
  195.         !exists(exe)                     // final program doesn't exist
  196.     )
  197.     {
  198.         printf("\n");
  199.         exec(COMPILER, "-o", exe, 
  200. #ifdef NO_LIBRARY
  201.             ofiles,
  202. #else
  203.         "-l" + library, 
  204. #endif
  205.         libs, "-L.", libpath, lopt);
  206. #ifndef GDB
  207.         exec("strip", exe);
  208. #endif
  209.         printf("ok: ", exe, "\n");
  210.     }
  211. }
  212. /*
  213.                             C C O M P I L E . I M
  214. */
  215.  
  216. void c_compile(list cfiles)
  217. {
  218.     string
  219.         nextfile;
  220.     int
  221.         i;
  222.                 
  223. #ifdef NO_LIBRARY
  224.     if (!exists("o"))
  225.         system("mkdir o");
  226.     
  227.     chdir ("o");    
  228. #endif
  229.                                                       
  230.     if (sizeof(cfiles))            // files to compile ?
  231.     {
  232.         printf("\ncompiling: ", current, "\n\n");
  233.                     // compile all files separately
  234.     for (i = 0; nextfile = element(i, cfiles); i++)
  235.         exec(COMPILER,
  236.         "-c "
  237.         COPT + " " +
  238. #ifdef NO_LIBRARY
  239.         copt + " ../" + nextfile);
  240. #else
  241.         copt + " " + nextfile);
  242. #endif
  243.  
  244.            relink = 1;
  245.     printf("\n");
  246.     }
  247.  
  248. #ifdef NO_LIBRARY
  249.     chdir("..");
  250. #endif
  251.                                                       
  252.     printf("ok: ", current, "\n");
  253. }
  254. /*
  255.                             U P D A T E L I . I M
  256. */
  257.  
  258. void updatelib(string library)
  259. {
  260.     list
  261.     arlist,
  262.         objlist;
  263.     string
  264.         to,
  265.         from;
  266.  
  267.     objlist = makelist("*.o");
  268.  
  269.     if (!sizeof(objlist))
  270.         return;
  271.  
  272.     printf("\n");
  273.     relink = 1;
  274.  
  275.     exec("ar", "rvs", library, "*.o");
  276.     exec("rm", "*.o");
  277.  
  278.     printf("\n");
  279. }
  280.  
  281. void prefix_class(string class_id)
  282. {
  283.     list
  284.     o_files;
  285.     string
  286.     o_file;
  287.     int
  288.     i;
  289.  
  290.     o_files = makelist("*.o");
  291.  
  292.     for (i = 0; o_file = element(i, o_files); i++)
  293.     exec("mv", o_file, class_id + o_file);
  294. }
  295. /*
  296.                                 S T D C P P . I M
  297. */
  298.  
  299. void std_cpp(string library)
  300. {
  301.     list
  302.         cfiles;
  303.  
  304. #ifdef BIFLEX
  305.     if (exists("biflex"))                  // subdir biflex exists
  306.         biflex();                          // make recent files
  307. #endif                       
  308.  
  309.     cfiles = file_list(wild, library);      // make list of all cpp-files
  310.  
  311.     c_compile(cfiles);                      // compile cpp-files
  312. }
  313.  
  314. /*
  315.                                 C P P M A K E . C
  316.  
  317.     CPP files are processed by stdmake.
  318.  
  319.     Arguments of CPPMAKE:
  320.  
  321.     cpp_make(
  322.         string mainfile,    : name of the main .cpp file, or "" for library
  323.                               maintenance
  324.         string library,     : name of the local library to use/create
  325.                 (without lib prefix, .a suffix
  326.                  if main is given here, libmain.a is created)
  327.         string exe,         : (path) name of the exe file to create
  328.         )
  329.  
  330.     Both mainfile and library MUST be in the current directory
  331. */
  332.  
  333. void cpp_make(string mainfile, string library, string exe)
  334. {
  335.     int
  336.         n,
  337.         index;
  338.     list
  339.         classes;
  340.         
  341.     ofiles = "o/*.o";                       // std set of o-files
  342.  
  343.     classes = strtok(CLASSES, " ");         // list of classes
  344.  
  345.     if (n = sizeof(classes))
  346.         ofiles += " */o/*.o";               // set ofiles for NO_LIBRARY
  347.  
  348.     wild = sources;
  349.                         // make library name
  350.     libxxxa = chdir(".") + "lib" + library + ".a";
  351.  
  352.                                             // first process all classes
  353.     for (index = 0; index < n; index++)
  354.     {                   
  355.         current = element(index, classes);  // next class to process
  356.         chdir(current);                     // change to directory
  357.  
  358.         current = "subdir " + current;
  359.         std_cpp (libxxxa);                // compile all files
  360.         chdir( "..");                     // go back to parent dir
  361.     }
  362.  
  363.  
  364.     current = "auxiliary " + wild + " files";
  365.     std_cpp (libxxxa);                    // compile all files in current dir
  366.     
  367.     for (index = 0; index < n; index++)
  368.     {
  369.         current = element(index, classes);  // determine class name
  370.         chdir( current);                  // chdir to a class directory.
  371. #ifndef NO_LIBRARY
  372.     prefix_class((string)index);      // prefix class-number for .o files
  373.         updatelib(libxxxa);
  374. #endif
  375.         chdir( "..");                     // go back to parent dir
  376.     }
  377.  
  378.     current = "";                           // no class anymore
  379.  
  380. #ifndef NO_LIBRARY
  381.     updatelib(libxxxa);                // update lib in current dir
  382. #endif 
  383.  
  384.     if (mainfile != "")                     // mainfile -> do link
  385.     {
  386.         link(library, exe);
  387.         printf
  388.     (
  389.         "\nProgram construction completed.\n"
  390.         "\n"
  391.     );
  392.     }
  393. }
  394. /*
  395.                         S E T L I B S . I M
  396. */
  397. void setlibs()
  398. {       
  399.     int
  400.         n,
  401.         index;
  402.     list
  403.         cut;
  404.         
  405.     cut = strtok(LIBS, " ");        // cut op libraries
  406.     n = sizeof(cut);
  407.     for (index = 0; index < n; index++)
  408.         libs += " -l" + element(index, cut);
  409.     
  410.     cut = strtok(LIBPATH, " ");     // cut up the paths
  411.     n = sizeof(cut);
  412.     for (index = 0; index < n; index++)
  413.         libpath += " -L" + element(index, cut);
  414. }
  415.  
  416.  
  417. int main()
  418. {
  419.  
  420.     echo(ECHO_REQUEST);
  421.  
  422.     sources = "*.c";
  423.  
  424.     setlibs();
  425.  
  426. #ifdef GDB
  427.     copt = "-g";
  428. #else
  429.     copt = "-O2";
  430. #endif
  431.  
  432. #ifdef PROGRAM
  433.     cpp_make
  434.     (
  435.         "cb.c",          // program source
  436.         "cb",                    // program library
  437.         "cb"                     // binary program
  438.     );
  439. #else
  440.     cpp_make
  441.     (
  442.         "",
  443.         "cb",                    // program library
  444.         ""
  445.     );
  446. #endif
  447.     return (0);
  448. }
  449.